// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   check_ok.java

package ecommerce.website.contact;

import com.jspsmart.upload.*;
import include.get_sql.getInsertSql;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class check_ok extends HttpServlet
{

	public check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		try
		{
			HttpSession dbSession = request.getSession();
			JspFactory _jspxFactory = JspFactory.getDefaultFactory();
			javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
			ServletContext dbApplication = dbSession.getServletContext();
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			nseer_db_backup1 ecommerce_db = new nseer_db_backup1(dbApplication);
			ValidataNumber validata = new ValidataNumber();
			ValidataTag vt = new ValidataTag();
			counter count = new counter(dbApplication);
			getInsertSql getInsertSql = new getInsertSql();
			getAttachmentLength getAttachmentLength = new getAttachmentLength();
			getAttachmentType getAttachmentType = new getAttachmentType();
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.setCharset("UTF-8");
			if (ecommerce_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				mySmartUpload.initialize(pageContext);
				String file_type = getFileLength.getFileType((String)session.getAttribute("unit_db_name"));
				long d = getFileLength.getFileLength((String)session.getAttribute("unit_db_name"));
				mySmartUpload.setMaxFileSize(d);
				mySmartUpload.setAllowedFilesList(file_type);
				String contact_ID = request.getParameter("contact_ID");
				String config_ID = request.getParameter("config_ID");
				try
				{
					mySmartUpload.upload();
					String filekind_chain = mySmartUpload.getRequest().getParameter("filekind_chain");
					String chain_id = Divide1.getId(filekind_chain);
					String chain_name = Divide1.getName(filekind_chain);
					String tel = mySmartUpload.getRequest().getParameter("tel");
					String address = mySmartUpload.getRequest().getParameter("address");
					String fax = mySmartUpload.getRequest().getParameter("fax");
					String postcode = mySmartUpload.getRequest().getParameter("postcode");
					String qq1 = mySmartUpload.getRequest().getParameter("qq1");
					String qq2 = mySmartUpload.getRequest().getParameter("qq2");
					String qq3 = mySmartUpload.getRequest().getParameter("qq3");
					String qq4 = mySmartUpload.getRequest().getParameter("qq4");
					String email = mySmartUpload.getRequest().getParameter("email");
					String checker_ID = mySmartUpload.getRequest().getParameter("checker_ID");
					String checker = mySmartUpload.getRequest().getParameter("checker");
					String check_time = mySmartUpload.getRequest().getParameter("check_time");
					String file_name[] = new String[mySmartUpload.getFiles().getCount()];
					String not_change[] = new String[mySmartUpload.getFiles().getCount()];
					String sql6 = (new StringBuilder("select id from ecommerce_workflow where object_ID='")).append(contact_ID).append("' and ((check_tag='0' and config_ID<'").append(config_ID).append("') or (check_tag='1' and config_ID='").append(config_ID).append("'))").toString();
					ResultSet rs6 = ecommerce_db.executeQuery(sql6);
					if (!rs6.next())
					{
						if (vt.validata((String)session.getAttribute("unit_db_name"), "ecommerce_contact", "contact_ID", contact_ID, "check_tag").equals("0"))
						{
							String sql1 = (new StringBuilder("select * from ecommerce_contact where contact_ID='")).append(contact_ID).append("'").toString();
							ResultSet rs = ecommerce_db.executeQuery(sql1);
							rs.next();
							String attachment[] = mySmartUpload.getRequest().getParameterValues("attachment");
							String delete_file_name[] = new String[0];
							if (attachment != null)
							{
								delete_file_name = new String[attachment.length];
								for (int i = 0; i < attachment.length; i++)
									delete_file_name[i] = rs.getString(attachment[i]);

							}
							for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
							{
								SmartFile file = mySmartUpload.getFiles().getFile(i);
								if (file.isMissing())
								{
									file_name[i] = "";
									int q = i + 1;
									String field_name = (new StringBuilder("attachment")).append(q).toString();
									if (!rs.getString(field_name).equals(""))
										not_change[i] = "yes";
								} else
								{
									int filenum = count.read((String)dbSession.getAttribute("unit_db_name"), "ecommerceAttachmentcount");
									count.write((String)dbSession.getAttribute("unit_db_name"), "ecommerceAttachmentcount", filenum);
									file_name[i] = (new StringBuilder(String.valueOf(filenum))).append(file.getFileName()).toString();
									file.saveAs((new StringBuilder(String.valueOf(path))).append("ecommerce/file_attachments/").append(filenum).append(file.getFileName()).toString());
								}
							}

							String sqla = (new StringBuilder("update ecommerce_contact set chain_id='")).append(chain_id).append("',chain_name='").append(chain_name).append("',email='").append(email).append("',tel='").append(tel).append("',fax='").append(fax).append("',address='").append(address).append("',postcode='").append(postcode).append("',qq1='").append(qq1).append("',qq2='").append(qq2).append("',qq3='").append(qq3).append("',qq4='").append(qq4).append("',checker_ID='").append(checker_ID).append("',checker='").append(checker).append("',check_time='").append(check_time).append("'").toString();
							String sqlb = (new StringBuilder(" where contact_ID='")).append(contact_ID).append("'").toString();
							if (attachment != null)
							{
								for (int i = 0; i < attachment.length; i++)
								{
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",").append(attachment[i]).append("=''").toString();
									File file = new File((new StringBuilder(String.valueOf(path))).append("ecommerce/file_attachments/").append(delete_file_name[i]).toString());
									file.delete();
								}

							}
							for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
								if (not_change[i] == null || !not_change[i].equals("yes"))
								{
									int p = i + 1;
									sqla = (new StringBuilder(String.valueOf(sqla))).append(",attachment").append(p).append("='").append(file_name[i]).append("'").toString();
								}

							String sql = (new StringBuilder(String.valueOf(sqla))).append(sqlb).toString();
							ecommerce_db.executeUpdate(sql);
							sql = (new StringBuilder("update ecommerce_workflow set check_tag='1',checker_ID='")).append(checker_ID).append("',checker='").append(checker).append("',check_time='").append(check_time).append("' where object_ID='").append(contact_ID).append("' and config_id='").append(config_ID).append("'").toString();
							ecommerce_db.executeUpdate(sql);
							sql6 = (new StringBuilder("select * from ecommerce_workflow where object_ID='")).append(contact_ID).append("' and check_tag='0'").toString();
							rs6 = ecommerce_db.executeQuery(sql6);
							if (!rs6.next())
							{
								sql = (new StringBuilder("update ecommerce_contact set check_tag='1',checker_ID='")).append(checker_ID).append("',checker='").append(checker).append("',check_time='").append(check_time).append("' where contact_ID='").append(contact_ID).append("'").toString();
								ecommerce_db.executeUpdate(sql);
							}
							response.sendRedirect("ecommerce/website/contact/check_ok.jsp?finished_tag=0");
						} else
						{
							response.sendRedirect("ecommerce/website/contact/check_ok.jsp?finished_tag=1");
						}
					} else
					{
						response.sendRedirect("ecommerce/website/contact/check_ok.jsp?finished_tag=2");
					}
					ecommerce_db.commit();
					ecommerce_db.close();
				}
				catch (Exception ex)
				{
					response.sendRedirect((new StringBuilder("ecommerce/website/contact/check_ok_a.jsp?contact_ID=")).append(contact_ID).toString());
				}
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
